﻿namespace STLTypeVisualizer
{
	using System;
	using VisualGDBExpressions;

	internal abstract class PtrFilter : TypeListBasedExpressionFilter
	{
		public PtrFilter(params string[] supportedTypes) : base(supportedTypes)
		{
		}

		protected override IExpression DoAttach(IExpression expr, IExpressionEvaluator evaluator)
		{
			bool flag1;
			string fullNameForEvaluator = expr.FullNameForEvaluator;
			IExpression expression = evaluator.CreateExpression(GetExpressString(evaluator, fullNameForEvaluator), true);
			if (expression == null)
			{
				return null;
			}
			ExpressionValue.Integral integral1 = expression.Value as ExpressionValue.Integral;
			if (integral1 != null)
			{
				flag1 = integral1.Value == 0L;
			}
			else
			{
				ExpressionValue.Integral local1 = integral1;
				flag1 = false;
			}
			if (!flag1)
			{
				return expression;
			}
			StaticExpressionFilter filter1 = new StaticExpressionFilter(expr);
			filter1.ValueOverride = new ExpressionValue.Custom("空指针");
			return filter1;
		}

		protected abstract string GetExpressString(IExpressionEvaluator evaluator, string fullNameForEvaluator);
	}

	internal class GccUniquePtrFilter : PtrFilter
	{
		static string[] textArray1 = new string[] { "std::unique_ptr<" };
		public GccUniquePtrFilter() : base(textArray1)
		{
		}

		protected override string GetExpressString(IExpressionEvaluator evaluator, string fullNameForEvaluator)
		{
			// 低版本使用的数据结构
			IExpression expression = evaluator.CreateExpression($"({fullNameForEvaluator})._M_t._M_head_impl", true);
			if (expression != null)
			{
				return $"({fullNameForEvaluator})._M_t._M_head_impl";
			}

			// 高版本使用的数据结构
			return $"({fullNameForEvaluator})._M_t._M_t._M_head_impl";
		}
	}

	internal class GccSharedPtrFilter : PtrFilter
	{
		static string[] textArray1 = new string[] { "std::shared_ptr<" };
		public GccSharedPtrFilter() : base(textArray1)
		{
		}

		protected override string GetExpressString(IExpressionEvaluator evaluator, string fullNameForEvaluator)
		{
			return $"({fullNameForEvaluator})._M_ptr";
		}
	}

	internal class ClangUniquePtrFilter : PtrFilter
	{
		static string[] textArray1 = new string[] { "std::__1::unique_ptr<" };
		public ClangUniquePtrFilter() : base(textArray1)
		{
		}

		protected override string GetExpressString(IExpressionEvaluator evaluator, string fullNameForEvaluator)
		{
			return $"({fullNameForEvaluator}).__ptr_.__value_";
		}
	}

	internal class ClangSharedPtrFilter : PtrFilter
	{
		static string[] textArray1 = new string[] { "std::__1::shared_ptr<" };
		public ClangSharedPtrFilter() : base(textArray1)
		{
		}

		protected override string GetExpressString(IExpressionEvaluator evaluator, string fullNameForEvaluator)
		{
			return $"({fullNameForEvaluator}).__ptr_";
		}
	}
}
